<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/29
 * Time: 10:25
 */

namespace app\mocks\extend;


use think\Env;

class MockRedis
{
    protected static $instance = null;

    private function __construct()
    {
    }

    public static function instance()
    {
        if (!Env::get('mock.redis')) {
            return null;
        }
        if (self::$instance == null || (!self::$instance instanceof MockRedis)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function get($key)
    {
        $str = Env::get('mock_redis_data.'.$key);
        if (!$result = \json_decode($str, true)) {
            if (strpos($str, ',')) {
                $data = explode(',', $str);
                foreach ($data as $item) {
                    if ($item) {
                        $tmp                       = explode(':', $item);
                        $result[ trim($tmp[ 0 ]) ] = trim($tmp[ 1 ]);
                    }
                }
            } elseif (strpos($str, ':')) {
                $data = explode(',', $str);
                foreach ($data as $item) {
                    if ($item) {
                        $tmp                       = explode(':', $item);
                        $result[ trim($tmp[ 0 ]) ] = trim($tmp[ 1 ]);
                    }
                }
            } else {
                $result = [$key => $str];
            }
        }
        if ($result) {
            return json_encode($result, JSON_UNESCAPED_UNICODE);
        } else {
            return 'false';
        }
    }

    public function set($key, $val)
    {
        return true;
    }

    public function connect($host, $port)
    {
        return true;
    }

    public function select($index)
    {
        return true;
    }

    public function expire($key, $second)
    {
        return true;
    }

    public function expireAt($key, $second)
    {
        return true;
    }

    public function LPUSH($key, $value)
    {
        return true;
    }

    public function RPUSH($key, $value)
    {
        return true;
    }

    public function lrange($key, $head, $tail)
    {
        return [];
    }

    public function rpop($key)
    {
        return true;
    }

    public function lpop($key)
    {
        return true;
    }

    public function sadd($key, $value)
    {
        return true;
    }

    public function exists($key)
    {
        return true;
    }

    public function zadd($key, $score, $value)
    {
        return true;
    }

    public function zrevrange($key, $min, $max)
    {
        return [];
    }

    public function zrange($key, $min, $max)
    {
        return [];
    }

    public function zrevrank($key, $member)
    {
        return [];
    }

    public function zrank($key, $member)
    {
        return [];
    }

    public function ZRANGEBYSCORE($key, $star, $end)
    {
        return [];
    }

    public function ZSCORE($key, $member)
    {
        return '';
    }

    public function hset($key, $field, $value)
    {
        return true;
    }

    public function hexists($id)
    {
        return true;
    }

    public function hGet($id)
    {
        return '';
    }

    public function hvals($id)
    {
        return '';
    }

    public function hkeys($id)
    {
        return '';
    }
    // todo 还有一些方法没有mock，懒得写了
    public function hMget($data)
    {
        return '';
    }

}